import { imgToBase64, Jump } from '@/utils/tools.js'

/**
 * @description APP端压缩图片  采用uni.compressImage   
 * 				H5端采用 动态创建canvas方式压缩图片 
 * 				H5传入文件对象  需要获取文件类型和名称 UNI中chooseImage选择【tempFiles】
 * @@param {file,base64} tempImagePath 图片地址
 * 			
 * @@param {String} imageType 转出的图片格式 默认base64
 **/
export async function compressImage(tempImage, imageType = 'base64') {
	let tempImagePath = tempImage;
	// #ifdef H5
	//H5转换时 需要拿到 tempImage.type  也就是文件类型 以免canvas转出时影响图片源
	tempImagePath = tempImage.path
	// #endif
	const { targetWidth, targetHeight } = await imageScale(tempImagePath)
	// #ifdef APP-PLUS
	let patha = await uniCompressImage(tempImagePath, targetWidth, targetHeight);
	if (imageType == 'base64') {
		return await imgToBase64(patha);
	} else {
		return patha
	}
	// #endif
	// #ifdef H5
	let img = new Image()
	img.src = tempImagePath;
	let canvas = document.createElement('canvas');
	let ctx = canvas.getContext('2d');
	canvas.width = targetWidth;
	canvas.height = targetHeight;
	// 图片压缩
	ctx.drawImage(img, 0, 0, targetWidth, targetHeight)
	// canvas对图片进行缩放 0.3是我定义的图片质量，
	let base64 = canvas.toDataURL(tempImage.type, 1);
	if (imageType == 'base64') {
		return base64
	} else {
		// 将base64转换为filel流，
		let flie = convertBase64UrlToFile({
			dataURL: base64,
			type: tempImage.type,
			contentName: tempImage.name
		})
		return file
	}
	// #endif
}
/**
 * @param {file,base64} tempImagePath 图片地址
 * @param {String/Number} maxWidth 图片最大宽度  默认1080
 * @param {String/Number} maxHeight 图片最大高度 默认1920
 * @@return {type} targetWidth targetHeight  计算后的宽高
 **/
///计算缩小比例
function imageScale(tempImagePath, maxWidth = 1080, maxHeight = 1920) {
	return new Promise(async (resolve, reject) => {
		uni.getImageInfo({
			src: tempImagePath,
			success: ({ width, height }) => {
				// 目标尺寸
				let targetWidth = width;
				let targetHeight = height;
				if (width > maxWidth || height > maxHeight) {
					if (width / height > maxWidth / maxHeight) {
						// 更宽，按照宽度限定尺寸
						targetWidth = maxWidth;
						targetHeight = Math.round(maxWidth * (height / width));
					} else {
						targetHeight = maxHeight;
						targetWidth = Math.round(maxHeight * (width / height));
					}
				}
				resolve({ targetWidth, targetHeight })
			},
			fail(e) {
				console.log(e)
			}
		})
	})
}

function uniCompressImage(tempImagePath1, targetWidth, targetHeight) {
	return new Promise(async (resolve, reject) => {
		uni.compressImage({
			src: tempImagePath1,
			quality: 100,
			compressedWidth: targetWidth,
			compressHeight: targetHeight,
			success: res => {
				resolve(res.tempFilePath)

			}
		})
	})
}
function convertBase64UrlToFile(base64) {
	let urlData = base64.dataURL
	let type = base64.type
	let contentName = base64.contentName
	let bytes = null
	if (urlData.split(',').length > 1) { //是否带前缀
		bytes = window.atob(urlData.split(',')[1]) // 去掉url的头，并转换为byte
	} else {
		bytes = window.atob(urlData)
	}
	// 处理异常,将ascii码小于0的转换为大于0
	let ab = new ArrayBuffer(bytes.length)
	let ia = new Uint8Array(ab)
	for (let i = 0; i < bytes.length; i++) {
		ia[i] = bytes.charCodeAt(i)
	}
	let result = new Blob([ab], {
		type: type,
	})
	let result1 = new File([result], contentName, {
		type: type
	})
	result1.path = window.URL.createObjectURL(result)
	return result1
}